<?php
header("Content-Type: text/html; charset=iso-8859-1");
set_time_limit(3600);
$connexion = mysql_pconnect('localhost','root','mysql');
mysql_query("SET NAMES 'latin1';", $connexion);
mysql_query("SET CHARACTER SET 'latin1';", $connexion);
if(!$connexion){
	echo "Connexion &agrave; ". 'localhost' ." impossible\n";
	exit;
}

if(!mysql_select_db('vbcll',$connexion)){
	echo "Acc&egrave;s &agrave; la base test impossible\n";
	exit;
}

if (!isset($fichierExecRequete))
{
	$fichierExecRequete = 1;
	
	function ExecRequete ($requete)
	{
		$resultat = mysql_query ($requete);
		if ($resultat)
			return $resultat;
		else
			{
				echo "<p class = 'erreur'><span class = 'bold'>Erreur dans l'ex&eacute;cution de la requ&egrave;te</span>";
				echo "Requ&egrave;te : $requete.\n";
				echo "Message de MySQL : " . mysql_error();
				echo "</p>\n";
				error_log(mysql_error());
				exit;
			}
	}
	
	function LigneSuivante ($resultat)
	{
		return mysql_fetch_object ($resultat);
	}
	
}
function describe($table){
	$fields = array();
	$q = "desc $table";
	$r = ExecRequete($q);
	while($s = LigneSuivante($r)){
		$fields[$s->Field] = $s->Type;
	}
	return $fields;
}
/*
######### equipes ###########
echo "Equipes...";
$fields = describe("vbcll2.equipes");
$q = "delete from vbcll2.equipes";
ExecRequete($q);
$q = "delete from vbcll2.membre_equipes";
ExecRequete($q);
$q = "delete from vbcll2.membre_communications";
ExecRequete($q);

$q = "select * from equipes";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "replace into vbcll2.equipes set ";
	foreach($fields as $field => $type){
		if($field == 'id_vbcll') $q1 .= " $field = '".(addslashes($s->id))."', ";
		elseif($field != 'id' && $field != 'annee_max') $q1 .= " $field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
	$id = mysql_insert_id();
	if($s->entraineur){
		$q = "replace into vbcll2.entraineurs set equipe_id = '$id', membre_id = '$s->entraineur'";
		ExecRequete($q);
	}
	if($s->entraineur2){
		$q = "replace into vbcll2.entraineurs set equipe_id = '$id', membre_id = '$s->entraineur'";
		ExecRequete($q);
	}
	for($i = 1; $i < 4 ; $i++){
		if($s->{"entrainement".$i}){
			$q = "replace into vbcll2.entrainements set equipe_id = '$id', jour = '".$s->{"entrainement".$i}."', heure = '".$s->{"heure".$i}."', salle = '".$s->{"heure".$i}."'";
			ExecRequete($q);
		}		
	}

}
echo "done.\n";
####### membres #########
echo "Membres...";
$q = "delete from vbcll2.membres";
ExecRequete($q);
$q = "delete from vbcll2.membre_commissions";
ExecRequete($q);
*/
$q = "select * from membres where titre != 'NULL' and titre != ''";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
/*	$fields = describe('vbcll2.membres');
	$q1 = "replace into vbcll2.membres set ";
	foreach($fields as $field => $type){
		if($field == 'nom') $q1 .= " $field = '".ucwords(strtolower(addslashes($s->$field)))."', ";
		elseif($field != 'demission' && $field != 'valide') $q1 .= " $field = '".(addslashes($s->$field))."', ";
	}
	$q1 .= " valide = '1'";
	ExecRequete($q1);
	
	if($s->equipe1){
		$q1 = "select id from vbcll2.equipes where id_vbcll = '$s->equipe1'";
		$r1 = ExecRequete($q1);
		if($s1 = LigneSuivante($r1)){
			$equipe = $s1->id;
			$q1 = "replace into vbcll2.membre_equipes set membre_id = $s->id, equipe_id = $equipe";
			ExecRequete($q1);
		}
	}
	if($s->equipe2){
		$q1 = "select id from vbcll2.equipes where id_vbcll = '$s->equipe2'";
		$r1 = ExecRequete($q1);
		if($s1 = LigneSuivante($r1)){
			$equipe = $s1->id;
			$q1 = "replace into vbcll2.membre_equipes set membre_id = $s->id, equipe_id = $equipe";
			ExecRequete($q1);
		}
	}
	if($s->commission_technique){
		$q1 = "replace into vbcll2.membre_commissions set membre_id = $s->id, commission_id = 2, fonction = '".(addslashes($s->commission_technique))."'";
		ExecRequete($q1);
	}
	if($s->commission_lna){
		$q1 = "replace into vbcll2.membre_commissions set membre_id = $s->id, commission_id = 3, fonction = 'responsable'";
		ExecRequete($q1);
	}

	$q1 = "select fonction, membre_id from comite left join membres_comite on comite.id = membres_comite.comite_id";
	$r1 = ExecRequete($q1);
	while($s1 = LigneSuivante($r1)){
		$q1 = "replace into vbcll2.membre_commissions set membre_id = $s1->membre_id, commission_id = 1, fonction = '$s1->fonction'";
		ExecRequete($q1);
		
	}
*/
	$fields = describe("vbcll2.comptabilite");
	$q1 = "replace into vbcll2.comptabilite set membre_id = '$s->id', ";
	foreach($fields as $field => $type){
		if($field != 'id' && $field != 'membre_id'){
			$q1 .= "$field = '".(addslashes($s->$field))."', ";
		}
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}
/*
$fields = describe('vbcll2.membres');
$q = "select * from ancien_membres where titre != 'NULL' and titre != ''";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "replace into vbcll2.membres set ";
	foreach($fields as $field => $type){
		if($field == 'nom') $q1 .= " $field = '".ucwords(strtolower(addslashes($s->$field)))."', ";
		elseif($field == 'demission') $q1 .= " $field = '".$s->date_demission."', ";
		elseif($field != 'valide') $q1 .= " $field = '".(addslashes($s->$field))."', ";
	}
	$q1 .= " valide = '0'";
	ExecRequete($q1);
}		

echo "done.\n";

############ Arbitrages ################
echo "Arbitrages...";
$fields = describe('arbitrages');
$q = "delete from vbcll2.arbitrages";
ExecRequete($q);

$q = "select * from arbitrages";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "replace into vbcll2.arbitrages set ";
	foreach($fields as $field => $type){
		$q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}

echo "done.\n";
########### Communications ##########
echo "Communications...";
$fields = describe('vbcll2.communications');
$q = "delete from vbcll2.communications";
ExecRequete($q);

$q = "select * from communications";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "replace into vbcll2.communications set ";
	foreach($fields as $field => $type){
		if($field == 'membre_id') $q1 .= "$field = '".(addslashes($s->auteur))."', ";
		elseif($field == 'equipe_id'){
			$q2 = "select id from vbcll2.equipes where id_vbcll = '$s->equipe'";
			$r2 = ExecRequete($q2);
			$equipe = ($s2 = LigneSuivante($r2)) ? $s2->id : 0;
			$q1 .= "$field = '$equipe', ";
		}
		else $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}
echo "done.\n";


######### Classements ############
echo "classements...";
$fields = describe('`vbcll2.classements`');
$q = "delete from `vbcll2.classements`";
ExecRequete($q);

$q = "select * from classements";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "replace into `vbcll2.classements` set ";
	foreach($fields as $field => $type){
		if($field == 'id_vbcll'){
			$q2 = "select id from equipes where id_vbcll = '$s->id_vbcll'";
			$r2 = ExecRequete($q2);
			$id_vbcll = ($s2 = LigneSuivante($r2)) ? $s2->id : 0;
		}
		else $$field = $s->$field;
		$q1 .= $field ." = '".(addslashes($$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);	
}
echo "done. \n";

######## Historique ##################
echo "historique...";
$fields = describe('vbcll2.historique');
$q = "delete from vbcll2.historique";
ExecRequete($q);

$q = "select * from historique order by id desc";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	// error_log($s->id." => ".$s->periode);
	$journal = $s->titre;
	if(ereg("<h2>(.*)<\/h2>(.*)",$s->corps,$regs)){
		$titre = $regs[1];
		$corps = str_replace("http://www.vbcll.ch/","",$regs[2]);
	}
	else{
		$titre = "sans titre";
		$corps = str_replace("http://www.vbcll.ch/","",$s->corps);
	}
	$periode = $s->periode;
	$valide = $s->valide;
	$date = $s->date;
	$id = $s->id;
	$q1 = "replace into vbcll2.historique set ";
	foreach($fields as $field => $type){
		$q1 .= $field ." = '".(addslashes($$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);	
}

$fields = describe('presse');
$q = "delete from vbcll2.presse";
ExecRequete($q);

$q = "select * from presse";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "replace into vbcll2.presse set ";
	foreach($fields as $field => $type){
		$q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}
echo "done. \n";

########## Infos ##############
echo "Infos...";
$fields = describe('vbcll2.infos');
$q = "delete from vbcll2.infos";
ExecRequete($q);

$q = "select * from infos";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.infos set ";
	foreach($fields as $field => $type){
		if($field == 'membre_id') $q1 .= "$field = '".(addslashes($s->auteur))."', ";
		elseif($field == 'small'){
			$small = (strlen($s->corps) < 500) ? 0 : 0;
			$q1 .= "$field = '$small', ";
		}
		elseif($field == 'corps') $q1 .= "$field = '".(addslashes(str_replace("http://www.vbcll.ch/images","images/infos",$s->corps)))."', ";
		elseif($field == 'datetime') $q1 .= "$field = '".(addslashes($s->date))."', ";
		else $q1 .= "$field = '".(addslashes($s->$field))."', ";		
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}
echo "done.\n";

######### Matchs ##############
echo "Matchs...";
$fields = describe('vbcll2.matchs');
$q = "delete from vbcll2.matchs";
ExecRequete($q);

$q = "select * from matchs";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.matchs set ";
	foreach($fields as $field => $type){
		if($field == 'marqueur1') $q1 .= "$field = '".(addslashes($s->marqueur))."', ";
		elseif($field != 'marqueur2') $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}
$fields = describe('vbcll2.matchs_01_02');
$q = "delete from vbcll2.matchs_01_02";
ExecRequete($q);

$q = "select * from matchs_01_02";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.matchs_01_02 set ";
	foreach($fields as $field => $type){
		if($field == 'marqueur1') $q1 .= "$field = '".(addslashes($s->marqueur))."', ";
		elseif($field != 'marqueur2') $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}

$fields = describe('vbcll2.matchs_02_03');
$q = "delete from vbcll2.matchs_02_03";
ExecRequete($q);

$q = "select * from matchs_02_03";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.matchs_02_03 set ";
	foreach($fields as $field => $type){
		if($field == 'marqueur1') $q1 .= "$field = '".(addslashes($s->marqueur))."', ";
		elseif($field != 'marqueur2') $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}

$fields = describe('vbcll2.matchs_03_04');
$q = "delete from vbcll2.matchs_03_04";
ExecRequete($q);

$q = "select * from matchs_03_04";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.matchs_03_04 set ";
	foreach($fields as $field => $type){
		if($field == 'marqueur1') $q1 .= "$field = '".(addslashes($s->marqueur))."', ";
		elseif($field != 'marqueur2') $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}

$fields = describe('vbcll2.matchs_04_05');
$q = "delete from vbcll2.matchs_04_05";
ExecRequete($q);

$q = "select * from matchs_04_05";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.matchs_04_05 set ";
	foreach($fields as $field => $type){
		if($field == 'marqueur1') $q1 .= "$field = '".(addslashes($s->marqueur))."', ";
		elseif($field != 'marqueur2') $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}

$fields = describe('vbcll2.matchs_05_06');
$q = "delete from vbcll2.matchs_05_06";
ExecRequete($q);

$q = "select * from matchs_05_06";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.matchs_05_06 set ";
	foreach($fields as $field => $type){
		if($field == 'marqueur1') $q1 .= "$field = '".(addslashes($s->marqueur))."', ";
		elseif($field != 'marqueur2') $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}

$fields = describe('vbcll2.matchs_06_07');
$q = "delete from vbcll2.matchs_06_07";
ExecRequete($q);
$q = "select * from matchs_06_07";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.matchs_06_07 set ";
	foreach($fields as $field => $type){
		if($field == 'marqueur1') $q1 .= "$field = '".(addslashes($s->marqueur))."', ";
		elseif($field != 'marqueur2') $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}
echo "done.\n";
############### Salles ##########
echo "Salles...";
$fields = describe('salles');
$q = "delete from vbcll2.salles";
ExecRequete($q);
$q = "select * from salles";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "insert into vbcll2.salles set ";
	foreach($fields as $field => $type){
		if($field != 'id' && $field != 'lon' && $field != 'lat') $q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}
echo "done.\n";
############ Partenaires ###############
echo "partenaires...";
$fields = describe('support');
$q = "delete from vbcll2.partenaires";
ExecRequete($q);
$q = "select * from support";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$q1 = "replace into vbcll2.partenaires set ";
	foreach($fields as $field => $type){
		$q1 .= "$field = '".(addslashes($s->$field))."', ";
	}
	$q1 = substr($q1,0,-2);
	ExecRequete($q1);
}
*/
echo "done.\n";

?>